package com.niodata.dp.core.context;

import com.niodata.dp.log.LogUtil;
import org.apache.log4j.Logger;

public class ContextProvider {

  private static final Logger logger = Logger.getLogger(ContextProvider.class);

  private static ContextFactory factory = null;

  public static void setFactory(ContextFactory contextFactory) {
    if (contextFactory == null) {
      throw new NullPointerException("ContextFactory f is null");
    }
    factory = contextFactory;
  }

  public static Context context() {
    try {
      Context context = factory == null ? null : factory.getContext();
      return context;
    } catch (Exception e) {
      LogUtil.logStackTrace(logger, e);
      return null;
    }
  }

  @FunctionalInterface
  public interface ContextFactory {

    Context getContext();
  }
}
